package com.appcheck.recording.listener.impl;

import java.util.Iterator;

import android.view.View;
import android.view.View.OnClickListener;

import com.appcheck.recording.ListenerManager;
import com.appcheck.recording.PreViewManager;
import com.appcheck.recording.WidgetHelper;
import com.appcheck.recording.action.impl.ClickAction;
import com.appcheck.recording.listener.AbstractListener;
import com.appcheck.recording.model.IdNamePair;
import com.appcheck.recording.utils.Logger;

public class ClickListener extends AbstractListener implements OnClickListener {

	private int b;
	
	public ClickListener(View paramView) {
		super(paramView, View.class, View.OnClickListener.class);
	}

	public ClickListener(View paramView, PreViewManager preViewManager) {
		super(paramView, View.class, View.OnClickListener.class);
		super.setPreViewManager(preViewManager);
	}

	@Override
	public void onClick(View v) {
		if (this.b++ > 1) {
		      return;
		}
	    ClickAction clickAction = new ClickAction();
	    clickAction.setLongClick(false);
	    clickAction.setViewType(WidgetHelper.getWidgetMapIndexOfView(v));
	    //此处改为同时记录resourceName还有text
	    clickAction.setResourceName(WidgetHelper.getResourceName(v));
	    clickAction.setResourceId(WidgetHelper.getResourceId(v));
	    clickAction.setText(WidgetHelper.getViewContent(v));
	    if(this.getPreViewManager() != null){
	    	clickAction.setPreViewArray(this.getPreViewManager().getIdNameList().toArray(new IdNamePair[this.getPreViewManager().getIdNameList().size()]));
	    	clickAction.setIndex(this.getPreViewManager().getIndexInAdapterView());
	    }
	    //clickAction.setIndex(WidgetHelper.getCurrentIndexOfView(v));
//	    if (clickAction.getResourceName() == null){
//	    	clickAction.setText(WidgetHelper.getViewContent(v));
//	      	if (clickAction.getText()== null) {
//	      		clickAction.setIndex(WidgetHelper.getCurrentIndexOfView(v));
//	      	}
//	    }
	    ListenerManager.getListenerManager().handleAction(clickAction);
	    if (this.listener != null) {
	      ((View.OnClickListener)this.listener).onClick(v);
	    }
	    this.b = 0;
	}

}
